package notify

import (
	"APT/api/notify/hook"
	"APT/internal/dao"
	"APT/internal/model/input/input_pay"
	"APT/internal/service"
	"context"
	"github.com/gogf/gf/v2/frame/g"
)

func (c *ControllerHook) MlilifePay(ctx context.Context, req *hook.MlilifePayReq) (res *hook.MlilifePayRes, err error) {
	g.Log().Info(ctx, req)
	if req.TradeState == "REFUND" {
		return
	}
	// 更新 微信 transaction_id
	if req.TradeState == "SUCCESS" {
		if _, err = dao.PmsTransaction.Ctx(ctx).
			Where(dao.PmsTransaction.Columns().TransactionSn, req.OutTradeNo).Data(g.Map{
			dao.PmsTransaction.Columns().PaymentRequestId: req.TransactionID,
		}).Update(); err != nil {
			return
		}
	}

	if err = service.PayService().ThirdPayCompleted(ctx, &input_pay.ThirdPayInp{
		//TransNo:   req.Resource.SupplementaryData.RelatedIds.OrderID,
		TransNo: req.TransactionID,
	}); err != nil {
		g.Log().Path("logs/HOOK/PAYPAL_HOOK").Error(ctx, err)
		return
	}

	return
}
